<template>
         <view class="goods-list">
            <view v-for="(goods, i) in goodLists" :key="i" @click="gotoDetail(goods)">
            <my-good-list :goods="goods"></my-good-list>
            </view>
          </view>
</template>

<script>
  import { debounce } from 'lodash'
  export default {
    name: 'GoodList',
    components:{
    },
    data() {
      return {
        queryObj: {
          query: '',
          cid: '',
          pagenum: 1,
          pagesize: 10
        },
        isloading: false,
        total:0 ,// 总条数
        goodLists: [], // 商品列表
        // 默认的空图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
      }
    },
    created(){
    // this.queryObj.cid = this.$route.query.cid
    },
    mounted(){
    },
    // 上啦触底
    onReachBottom(){
      // 根据这个阀判断是否发起数据 只要是false 就发起数据
     if(this.isloading) return
       this.getGoodLists()
    },
    onLoad(options) {
      console.log('options',options)
      if(options.query)  this.queryObj.query = options.query
      if(options.cid) this.queryObj.cid = options.cid
      this.getGoodLists()
    },
    // 下拉刷新
   async onPullDownRefresh(){
      this.queryObj.pagenum = 1,
      this.total = 0,
      this.goodLists = [],
      await this.getGoodLists()
      uni.stopPullDownRefresh()
    },
    methods:{
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      getGoodLists: debounce(async function(){
       if(this.goodLists.length == this.total &&  this.total !== 0) return
        this.isloading =  true
       let {data: {message, meta: {status}}} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        this.isloading = false
       if(status!== 200) return uni.$showMsg()
       this.goodLists = [...this.goodLists,...message.goods]
       this.queryObj.pagenum++
       this.total = message.total
      },1000)
    }
  }
</script>

<style lang="scss">
 
</style>
